<?php
/**
 * 客户信息管理.
 * User: zhy
 * Date: 14-3-25
 * Time: 下午10:09
 * email:282477161@qq.com
 */

class ClientAction extends BaseAction{
    private $client;
    private $job;
    private $assistant;

    public function __construct()
    {
        parent::__construct();
        $this->client = D('client/ClientInfo');//客户基本信息
        $this->job = array('工人','程序员','农盲');
        $this->assistant = array('一级','二级','三级');

    }

    /**
     * 客户基本信息
     */
    public function index()
    {
        $pageSize = $this->_post('numPerPage','intval',1);
        $name = $this->_post('name','trim');

        $map['is_del'] = 0;
        if(!empty($name)){
            $map['name'] = array('like',"%$name%");
        }

        $list = $this->client->getClient($map,$pageSize);

        $this->assign('pageSize',$pageSize);
        $this->assign('list',$list);
        $this->assign('job',$this->job);
        $this->display();
    }

   public function delClient()
   {
       $id = $this->_get('id');

       if(empty($id)){
           $data['statusCode'] = 301;
           $data['message'] = '请选择要删除的对象';
           echo json_encode($data);
           exit;
       }

       $return = $this->client->where(array('id'=>$id))->save(array('is_del'=>1));

       if($return){
           $data['statusCode'] = 200;
           $data['message'] = '删除成功';
       }else{
           $data['statusCode'] = 301;
           $data['message'] = '删除失败';
       }

       echo json_encode($data);
   }

    //添加客户信息
    public function add()
    {

        if(IS_POST){
            $lx = $this->_post('lt');
            $emo = $this->_post('emo');
            $peiou = $this->_post('peiou');
            $parent = $this->_post('parent');
            $children = $this->_post('children');

            $this->client->create();

            $this->client->tel = json_encode($lx);
            $this->client->emo_info = json_encode($emo);
            $this->client->spouse = json_encode($peiou);
            $this->client->parents = json_encode($parent);
            $this->client->children = json_encode($children);


            $return = $this->client->add();
            if($return){
                $data['statusCode'] = 200;
                $data['message'] = '添加成功';
            }else{
                $data['statusCode'] = 301;
                $data['message'] = '添加失败';
            }

            echo json_encode($data);
            exit;
        }
        $this->assign('job',$this->job);
        $this->assign('assistant',$this->assistant);
        $this->display();
    }

    //客户回收站
    public function clientRemove()
    {
        $pageSize = $this->_post('numPerPage','intval',1);
        $name = $this->_post('name','trim');

        $map['is_del'] = 1;
        if(!empty($name)){
            $map['name'] = array('like',"%$name%");
        }

        $list = $this->client->getClient($map,$pageSize);

        $this->assign('pageSize',$pageSize);
        $this->assign('list',$list);
        $this->display();
    }

    //编辑
    public function editClient()
    {
        if(IS_POST){
            $id = $this->_post('clientId','intval');
            $lx = $this->_post('lt');
            $emo = $this->_post('emo');
            $peiou = $this->_post('peiou');
            $parent = $this->_post('parent');
            $children = $this->_post('children');
            $birthday = $this->_post('birthday','trim');
            if(empty($id)){
                $data['statusCode'] = 301;
                $data['message'] = '请选择要删除的对象';
                echo json_encode($data);
                exit;
            }

            $this->client->create();

            $this->client->tel = json_encode($lx);
            $this->client->emo_info = json_encode($emo);
            $this->client->spouse = json_encode($peiou);
            $this->client->parents = json_encode($parent);
            $this->client->children = json_encode($children);
            $this->client->birthday = strtotime($birthday);


            $return = $this->client->where(array('id'=>$id))->save();

            if($return){
                $data['statusCode'] = 200;
                $data['message'] = '编辑成功';
            }else{
                $data['statusCode'] = 301;
                $data['message'] = '编辑失败';
            }
            echo json_encode($data);
            exit;
        }else{
            $id = $this->_get('id','intval');
            if(empty($id)){
                $data['statusCode'] = 301;
                $data['message'] = '请选择要删除的对象';
                echo json_encode($data);
                exit;
            }

            $data = $this->client->getListClient($id);

            if(!empty($data)){
                $data['tel'] = json_decode($data['tel'],true);
                $data['emo_info'] = json_decode($data['emo_info'],true);
                $data['spouse'] = json_decode($data['spouse'],true);
                $data['parents'] = json_decode($data['parents'],true);
                $data['children'] = json_decode($data['children'],true);
            }


            $this->assign('data',$data);
            $this->assign('job',$this->job);
            $this->assign('assistant',$this->assistant);
            $this->display('edit');
        }



    }

    public function jsonArray($arr)
    {
        $item = array();
        foreach($arr as $key=>$value){
            foreach($value as $k=>$v){
                $item[$k][$key] = $v;
            }
        }
        return json_encode($item);
    }

    public function jsonArrayTwo($arr)
    {
        $item = array();
        foreach($arr as $key=>$value){
            $item[$key] = $value;
        }
        return json_encode($item);
    }

} 